//处理每个数字方块的脚本，包括了碰撞逻辑
cc.Class({
    extends: cc.Component,

    properties: {
        num: 0,//cell里面的数值
        star: 0,//是否里面有星星
        easy: 0,//是否里面有轻松过关的元素
        label: {
            default: null,
            type: cc.Label
        },
        startlayer: {
            default: null,
            type: cc.Node
        },
        starlabel: {
            default: null,
            type: cc.Label
        }
    },

    //红色 橙色 黄色 绿色（标记快捷通道），这里计算颜色都是通过value的倍数来计算的，最小值是浅黄色，最大值是深色
    createNum: function (num,min,max,star,easy,game) {
        this.star = star;
        this.easy=easy;
        this._game = game;
        this._min=min;
        this._max=max;
        if(easy){
            this.num = parseInt(num/3);
        }else{
            this.num = num;
        }
        this._curNum=this.num;
        if (this.star == 1) {
            this.label.node.active = false;
            this.startlayer.active = true;
            this.starlabel.string = this._curNum.toString();
        } else {
            this.label.node.active = true;
            this.startlayer.active = false;
            this.label.string = this._curNum.toString();
        }
        this.setColor(true);
    },

    attack: function (hp) {
        let score=0;
        if (this._curNum - hp < 0) {
            this._curNum = 0;
            score=this._curNum;
        } else {
            this._curNum -= hp;
            score=hp;
        }
        if (this.star == 1) {
            this.starlabel.string = this._curNum.toString();
        } else {
            this.label.string = this._curNum.toString();
        }
        //这里还需要设置颜色
        if(this._curNum==0){
            if(this.star!=1){
                this._game.removeCell(this.node,this._initColor);
            }else{
                this._game.removeAllCells();
                return;
            }
        }else{
            this._game.soundHelper().playHit();
            this.setColor(false);
        }
        this._game.addScores(score);
    },

    setColor: function (init) {
        if(init&&this.easy){
            //如果是很简单的话直接
            this.node.color=cc.color(166,233,19);
        }else{
            let xishu=(this._curNum-this._min)/(this._max-this._min)*100;
            if(xishu<=33){
                //如果是最小的话，那就最浅的颜色
                this.node.color=cc.color(239,198,13);
            }else if(xishu<=66){
                this.node.color=cc.color(225,132,0);
            }else{
                this.node.color=cc.color(225,0,0);
            }
        } 
        if(init){
            this._initColor=this.node.color;
        }
    },

    onCollisionEnter: function (other) {
        if (other.node.group == "role") {
            //如果碰到了role
            if(other.tag==1){
                //顶部碰撞了,判断是否结束游戏
                this._game.checkDie(this.node);
            }else if(other.tag==2){
                other.node.tag=2;
                this._game.resetRolePos(this.node,true);
            }else if(other.tag==3){
                other.node.tag=3;
                this._game.resetRolePos(this.node,false);
            }
        } else if (other.node.group == "bullet") {
            //如果是碰到了子弹,这里直接拿子弹的攻击力
            let power=other.getComponent("bullet").power;
            this.attack(power);
            this._game.removeBullet(other.node);
        }
    },

    onCollisionExit: function (other) {
        if (other.node.group == "role") {
            //如果碰到了role
            if(other.tag==other.node.tag){
                other.node.tag=0;
            }
        }
    },

    // LIFE-CYCLE CALLBACKS:
    onLoad: function () {
        this.label.string = this.num.toString();
        // //把碰撞区域调节一下
        // let comp= this.getComponent(cc.BoxCollider);
        // comp.size=cc.size(comp.size.width-8,comp.size.height);
    },

    start: function () {

    },

    // update (dt) {},
});
